package com.amber.proyecto.envia.imagenes.sw.usuario;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.amber.proyecto.envia.imagenes.sw.Principal;
import com.amber.proyecto.envia.imagenes.sw.R;
import com.amber.proyecto.envia.imagenes.sw.utils.Cifrado;
import com.amber.proyecto.envia.imagenes.sw.utils.Variables;

public class PrincipalUsuario extends Activity{
	private Button ivCerrarSesion;
	private Button btnVerFotos;
	private Bundle bundle;
	private String contra;
	 private AlertDialog alertDialog;
	 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.principalusuario);
		
		bundle = getIntent().getExtras();
		ivCerrarSesion = (Button)findViewById(R.id.btnCerrarSesPrinUsu);
		ivCerrarSesion.setOnClickListener(ivCerrarPres);
		
		btnVerFotos = (Button)findViewById(R.id.btnVerFotosPrinUsu);
		btnVerFotos.setOnClickListener(btnVerFotosPres);
		
		Button btnCambiaContra = (Button)findViewById(R.id.btnCambiaContraPrinUsu);
		btnCambiaContra.setOnClickListener(btnCambiaContraPres);
		
		Button btnEliminaCta = (Button)findViewById(R.id.btnEliminaCuentaPrinUsu);
		btnEliminaCta.setOnClickListener(btnEliminaPres);
		
		//btnActualizaDatos = (Button)findViewById(R.id.btnActualizaPrinUsu);
		//btnActualizaDatos.setOnClickListener(btnActualizaDatosPres);
	}
	
	private OnClickListener btnEliminaPres = new OnClickListener() {
		
		public void onClick(View v) {
			mensajeElimina();
		}
	};
	
	private OnClickListener btnCambiaContraPres = new OnClickListener() {
		
		public void onClick(View v) {
			Intent intent = new Intent();
    		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);			
			intent.setClass(PrincipalUsuario.this, ActualizaContraUsu.class);
			intent.putExtra("correoUsuario", bundle.getString("correoUsuario"));
			startActivity(intent);
			finish();
			
		}
	};
	
	private OnClickListener btnVerFotosPres = new OnClickListener() {
		
		public void onClick(View v) {
			Intent intent = new Intent();
    		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);			
			intent.setClass(PrincipalUsuario.this, MainActivity.class);
			//intent.setClass(PrincipalUsuario.this, PaginacionImagenesUsuario.class);
			intent.putExtra("correoUsuario", bundle.getString("correoUsuario"));
			startActivity(intent);
			finish();
			
		}
	};
	private OnClickListener btnActualizaDatosPres = new OnClickListener() {
		
		public void onClick(View v) {
			Intent intent = new Intent();
    		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    		//Variables.inicioSesion = false;
    		intent.putExtra("correoUsuario", bundle.getString("correoUsuario"));
    		intent.setClass(PrincipalUsuario.this, ActualizaDatosUsuario.class);
       		startActivity(intent);
    		finish();			
		}
	};
	
	private OnClickListener ivCerrarPres = new OnClickListener() {

		public void onClick(View v) {
			Intent intent = new Intent();
    		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    		Variables.inicioSesion = false;
    		intent.setClass(PrincipalUsuario.this, Principal.class);
       		startActivity(intent);
    		finish();
		}
	};
	
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
    
	private void mensajeElimina(){

		final Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.setClass(PrincipalUsuario.this, Principal.class);
		
        LayoutInflater lInflater = LayoutInflater.from(PrincipalUsuario.this);
        View layout = lInflater.inflate(R.layout.dialogoelimina, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(PrincipalUsuario.this);
        builder.setView(layout);
        
        
        final EditText etContra = (EditText)layout.findViewById(R.id.etContraEliminaCta);

       builder.setPositiveButton("Conservar", new DialogInterface.OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			contra = etContra.getText().toString();
				if (contra.length() != 0){
				eliminaUsu(0);
				Variables.inicioSesion = false;
	       		startActivity(intent);
	    		finish();
			}
				else{
					Toast.makeText(PrincipalUsuario.this, "Captura la contraseña", Toast.LENGTH_LONG).show();
				}
			}
		})
	   .setNeutralButton("Cancelar", new DialogInterface.OnClickListener() {
		
		public void onClick(DialogInterface dialog, int which) {
			alertDialog.dismiss();
		}
	   })
       .setNegativeButton("Eliminar", new DialogInterface.OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			contra = etContra.getText().toString();
			if (contra.length() != 0){
				eliminaUsu(1);
				Variables.inicioSesion = false;
	       		startActivity(intent);
	    		finish();
			} 
			else{
				Toast.makeText(PrincipalUsuario.this, "Captura la contraseña", Toast.LENGTH_LONG).show();
			}
		}
	});
        
		
        alertDialog = builder.create();
        alertDialog.setTitle("Aviso");
        alertDialog.setCancelable(false);
        alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
        
        alertDialog.show();
	}
	
	
	
	private void eliminaUsu(int fotos){
		//fotos=0 conservar fotos=1 eliminar
		String URL = "http://"+Variables.HOST+"/pags/servicios.php";
		SoapObject request;
		String SOAP_ACTION="capeconnect:servicios:serviciosPortType#eliminaCuenta"; 
		String METHOD_NAME = "eliminaCuenta";
		String NAMESPACE = "http://www.your-company.com/servicios.wsdl";

		try{
			
			request = new SoapObject(NAMESPACE, METHOD_NAME); 
			request.addProperty("correoUsuario", bundle.getString("correoUsuario"));
			request.addProperty("contrasenia", Cifrado.getStringMessageDigest(contra,Cifrado.SHA256));

			request.addProperty("fotos", fotos);
			
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			
			envelope.dotNet = false;
			
			envelope.setOutputSoapObject(request);
			

			HttpTransportSE aht = new HttpTransportSE(URL); 

			aht.call(SOAP_ACTION, envelope);
			
			SoapObject result =  (SoapObject) envelope.bodyIn;
            SoapPrimitive spResult = (SoapPrimitive) result.getProperty("result");
			Log.i("resultado",spResult.toString());
			
            if (Integer.parseInt(spResult.toString()) > 0){
            	Toast.makeText(PrincipalUsuario.this, "Cuenta eliminada", Toast.LENGTH_LONG).show();
            }
            else{
            	Toast.makeText(PrincipalUsuario.this, "No se pudo eliminar la cuenta", Toast.LENGTH_LONG).show();
            }
            
	    } 
		catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
		
		finally{
			
		}
		
	}
}
